#include "Array2DDialog.h"
#include "Array2DHandler.h"
#include "Registry.h"
#include "mainwindow.h"
#include "ActorTable.h"
#include "CmdManager.h"

Array2DDialog::Array2DDialog(ArrayHandler* arrayHandler,QWidget *parent)
	: QDialog(parent)
{
	m_array2DHandler = arrayHandler;
	this->setUI();
}

Array2DDialog::~Array2DDialog()
{
}

void Array2DDialog::setUI()
{
	this->setWindowTitle(tr("阵列"));
	mainLayout = new QVBoxLayout;
	//第一行阵列方式互斥按钮组
	m_arrayLayout = new QHBoxLayout;
	rectButton = new QRadioButton(tr("矩形阵列"));
	m_arrayLayout->addWidget(rectButton);
	roundButton = new QRadioButton(tr("环形阵列"));
	m_arrayLayout->addWidget(roundButton);
	rectButton->setChecked(true);
	m_arrayTypeGroup = new QButtonGroup;
	m_arrayTypeGroup->addButton(rectButton,0);
	m_arrayTypeGroup->addButton(roundButton,1);
	m_arrayTypeGroup->setExclusive(true);
	pickButton = new QPushButton(tr("选择对象"));
	m_arrayLayout->addWidget(pickButton);
	mainLayout->addLayout(m_arrayLayout);
	//矩形阵列输入框
	rectLayout = new QGridLayout;
	rowNumberLabel = new QLabel(tr("行数:"));
	rowNumberLabel->setFixedSize(80, 20);
	rectLayout->addWidget(rowNumberLabel, 0, 0);
	rowDistanceLabel = new QLabel(tr("行间距:"));
	rowDistanceLabel->setFixedSize(80, 20);
	rectLayout->addWidget(rowDistanceLabel, 1, 0);
	columnNumberLabel = new QLabel(tr("列数:"));
	columnNumberLabel->setFixedSize(80, 20);
	rectLayout->addWidget(columnNumberLabel, 0, 2);
	columnDistanceLabel = new QLabel(tr("列间距:"));
	columnDistanceLabel->setFixedSize(80, 20);
	rectLayout->addWidget(columnDistanceLabel, 1, 2);
	ArrayCopyAngleLabel = new QLabel(tr("阵列角度:"));
	ArrayCopyAngleLabel->setFixedSize(80, 20);
	rectLayout->addWidget(ArrayCopyAngleLabel, 2, 1, Qt::AlignCenter);
	rowNumberEdit = new QLineEdit;
	rowNumberEdit->setFixedSize(80,20);
	rowNumberEdit->setText(tr("4"));
	rectLayout->addWidget(rowNumberEdit, 0, 1, Qt::AlignLeft);
	rowDistanceEdit = new QLineEdit;
	rowDistanceEdit->setFixedSize(80,20);
	rowDistanceEdit->setText(tr("2"));
	rectLayout->addWidget(rowDistanceEdit, 1, 1, Qt::AlignLeft);
	columnNumberEdit = new QLineEdit;
	columnNumberEdit->setFixedSize(80,20);
	columnNumberEdit->setText(tr("4"));
	rectLayout->addWidget(columnNumberEdit, 0, 3, Qt::AlignLeft);
	columnDistanceEdit = new QLineEdit;
	columnDistanceEdit->setFixedSize(80,20);
	columnDistanceEdit->setText(tr("2"));
	rectLayout->addWidget(columnDistanceEdit, 1, 3, Qt::AlignLeft);
	arrayCopyAngleEdit = new QLineEdit;
	arrayCopyAngleEdit->setFixedSize(80,20);
	arrayCopyAngleEdit->setText(tr("0"));
	rectLayout->addWidget(arrayCopyAngleEdit, 2, 2, Qt::AlignCenter);
	rectLayout->setSpacing(10);
	rectangleWidget = new QWidget;
	rectangleWidget->setLayout(rectLayout);
	m_stackWidget = new QStackedWidget;
	m_stackWidget->addWidget(rectangleWidget);
	//环形阵列输入框
	roundLayout = new QGridLayout;
	centerLabel = new QLabel(tr("中心点:"));
	centerLabel->setFixedSize(60, 20);
	roundLayout->addWidget(centerLabel, 0, 0);
	xLabel = new QLabel(tr("X坐标:"));
	xLabel->setFixedSize(40, 20);
	roundLayout->addWidget(xLabel, 0, 1);
	yLabel = new QLabel(tr("Y坐标:"));
	yLabel->setFixedSize(40, 20);
	roundLayout->addWidget(yLabel, 0, 3);
	totalCountLabel = new QLabel(tr("总数量:"));
	totalCountLabel->setFixedSize(100,40);
	roundLayout->addWidget(totalCountLabel, 1, 0);
	fillAngleLabel = new QLabel(tr("填充角度:"));
	fillAngleLabel->setFixedSize(100, 40);
	roundLayout->addWidget(fillAngleLabel, 2, 0);
	xCoordinateEdit = new QLineEdit;
	xCoordinateEdit->setFixedSize(60,20);
	xCoordinateEdit->setText(tr("0"));
	roundLayout->addWidget(xCoordinateEdit, 0, 2);
	yCoordinateEdit = new QLineEdit;
	yCoordinateEdit->setFixedSize(60,20);
	yCoordinateEdit->setText(tr("0"));
	roundLayout->addWidget(yCoordinateEdit, 0, 4);
	totalCountEdit = new QLineEdit;
	totalCountEdit->setFixedSize(60,20);
	totalCountEdit->setText(tr("4"));
	roundLayout->addWidget(totalCountEdit, 1, 1);
	fillAngleEdit = new QLineEdit;
	fillAngleEdit->setFixedSize(60,20);
	fillAngleEdit->setText(tr("360"));
	roundLayout->addWidget(fillAngleEdit, 2, 1);
	roundLayout->setSpacing(10);
	roundWidget = new QWidget;
	roundWidget->setLayout(roundLayout);
	m_stackWidget->addWidget(roundWidget);
	mainLayout->addWidget(m_stackWidget);
	//确认、取消、预览按钮
	m_buttonLayout = new QHBoxLayout;
	confirmButton = new QPushButton(tr("确定"));
	confirmButton->setFixedSize(60, 30);
	m_buttonLayout->addWidget(confirmButton);
	cancelButton = new QPushButton(tr("取消"));
	cancelButton->setFixedSize(60, 30);
	m_buttonLayout->addWidget(cancelButton);
	previewButton = new QPushButton(tr("预览"));
	previewButton->setFixedSize(60, 30);
	m_buttonLayout->addWidget(previewButton);
	m_buttonLayout->setSpacing(5);
	m_buttonLayout->setAlignment(Qt::AlignRight);
	mainLayout->addLayout(m_buttonLayout);
	setLayout(mainLayout);
	connect(rectButton,&QRadioButton::clicked,this,&Array2DDialog::rectangleClick);
	connect(roundButton,&QRadioButton::clicked,this,&Array2DDialog::roundClick);
	connect(pickButton,&QPushButton::clicked,this, &Array2DDialog::onSelect);
	connect(previewButton,&QPushButton::clicked,this,&Array2DDialog::preview);
	connect(confirmButton,&QPushButton::clicked,this,&Array2DDialog::confirm);
	connect(cancelButton,&QPushButton::clicked,this,&Array2DDialog::cancel);
}


void Array2DDialog::rectangleClick()
{
	switchWidget(0);
}
void Array2DDialog::roundClick()
{
	switchWidget(1);
}

void Array2DDialog::switchWidget(int index)
{
	m_stackWidget->setCurrentIndex(index);
}

void Array2DDialog::onSelect()
{
	m_array2DHandler->prompt(1,tr("请选择对象"));
	this->hide();
}

void Array2DDialog::preview()
{
	if (arrayCopy())
	{
		this->hide();
		m_array2DHandler->prompt(2, tr("返回[修改(M)/接受(A)]: "));
	}
	else
		cancel();
}

void Array2DDialog::confirm()
{
	if (arrayCopy())
	{
		m_array2DHandler->setStatus(4);
		cmdInput();
	}
	else
		cancel();
}

void Array2DDialog::cancel()
{
	m_array2DHandler->setStatus(5);
	cmdInput();
}

bool Array2DDialog::arrayCopy()
{
	int index = m_stackWidget->currentIndex();
	if (index == 0)
	{
		//矩形阵列
		int rowNumber = rowNumberEdit->text().toInt();
		double rowDistance = rowDistanceEdit->text().toDouble();
		int columnNumber = columnNumberEdit->text().toInt();
		double columnDistance = columnDistanceEdit->text().toFloat();
		if (rowNumber * columnNumber > 400)
			return false;
		float arrayCopyAngle = arrayCopyAngleEdit->text().toFloat();
		m_array2DHandler->rectCopy(rowNumber, rowDistance, columnNumber, columnDistance, arrayCopyAngle);
	}
	else
	{
		double xCoordinate = xCoordinateEdit->text().toDouble();
		double yCoordinate = yCoordinateEdit->text().toDouble();
		int totalCount = totalCountEdit->text().toInt();
		if (totalCount > 400)
			return false;
		float fillAngle = fillAngleEdit->text().toFloat();
		double angle = fillAngle / totalCount;
		m_array2DHandler->roundCopy(xCoordinate, yCoordinate, totalCount, angle);
	}
	return true;
}

void Array2DDialog::cmdInput()
{
	assert(m_array2DHandler);
	auto cmdManager = m_array2DHandler->cmdManager();
	cmdManager->input(true,CS_CMDLINE);
}

void Array2DDialog::keyPressEvent(QKeyEvent * event)
{
	switch (event->key())
	{
	case Qt::Key_Escape:
		this->cancel();
		break;
	}
}

void Array2DDialog::closeEvent(QCloseEvent *)
{
	cancel();
}

void Array2DDialog::setButtonEnable(bool flag)
{
	if (flag)
	{
		confirmButton->setEnabled(true);
		previewButton->setEnabled(true);
	}
	else
	{
		confirmButton->setEnabled(false);
		previewButton->setEnabled(false);
	}
}
